package com.caiwangdai.app.ui.user

import android.content.Context
import android.content.Intent
import com.caiwangdai.app.R
import com.caiwangdai.app.backend.RetrofitHome
import com.caiwangdai.app.backend.observer.OnDataV2Listener
import com.caiwangdai.app.backend.rx.RxSchedulers
import com.caiwangdai.app.bean.UserB
import com.caiwangdai.app.bean.base.BaseB
import com.caiwangdai.app.bean.base.NormalResponseB
import com.caiwangdai.app.extension.*
import com.caiwangdai.app.origin.KeyBoardObserverActivity
import com.caiwangdai.app.origin.ToolBarManager
import com.caiwangdai.app.utils.AuthUtils
import com.caiwangdai.app.widget.SMSCountDownTimer
import kotlinx.android.synthetic.main.activity_register.*

//注册界面
class RegisterActivity : KeyBoardObserverActivity() {
    override fun getLayoutResID() = R.layout.activity_register


    private var mobile: String = ""
    private var verifyCode: String = ""
    private var inviteCode: String = ""

    private lateinit var smsCounter: SMSCountDownTimer


    companion object {
        fun newInstance(context: Context): Intent {
            val intent = Intent(context, RegisterActivity::class.java)
            return intent
        }
    }


    override fun initView() {
        //短信倒计时
        smsCounter = SMSCountDownTimer(tv_send_code)

        //Toolbar
        ToolBarManager.with(this, WMContent)
                .setBackgroundColor(R.color.white)
                .setNavigationIcon(R.drawable.ic_nav_close_circle)

        //mobile
        et_mobile.setText(mobile)
        et_mobile.setSelectionEnd()

        //verify_code
        et_verify_code.setText(verifyCode)

        //invite_code
        et_invite_code.setText(inviteCode)

        //send code
        tv_send_code.setOnClickListenerPro { if (checkMobile()) getCode() }

        //btn submit
        btn_register.setOnClickListenerPro { if (checkValue()) actionRegister() }

        //tv contack
        tv_contact.setOnClickListener {
            val number = tv_contact.text.substring(5)
            copyContent(number) {
                toast("已粘贴至剪贴板")
                openWeiXin()
            }

        }
    }


    private fun checkValue(): Boolean {
        mobile = et_mobile.getContent()
        verifyCode = et_verify_code.getContent()
        inviteCode = et_invite_code.getContent()
        if (!checkMobile()) return false
        if (!smsCounter.getCodeIng) {
            toast("请先获取验证码")
            return false
        }
        if (verifyCode.isEmpty()) {
            toast("请输入验证码")
            return false
        }
        if (inviteCode.isEmpty()) {
            toast("请输入邀请码")
            return false
        }
        return true
    }

    private fun checkMobile(): Boolean {
        mobile = et_mobile.getContent()
        if (mobile.isEmpty()) {
            toast("请输入手机号码")
            return false
        }
        if (!AuthUtils.isMobileNo(mobile)) {
            toast("请确保手机号格式输入正确")
            return false
        }
        return true
    }


    private fun getCode() {
        RetrofitHome.getUserApi()
                .getVerifyCodeInRegister(mobile)
                .compose(RxSchedulers.toMain())
                .subscribe(object : OnDataV2Listener<BaseB>(this) {
                    override fun getProgressBarContent() = "获取验证码..."

                    override fun onSuccess(value: NormalResponseB<BaseB>) {
                        toast("获取验证码成功")
                        smsCounter.startCountTime()
                    }

                })
    }


    /**
     * 开始注册
     */
    private fun actionRegister() {
        RetrofitHome
                .getUserApi()
                .register(mobile, verifyCode, inviteCode)
                .compose(RxSchedulers.toMain())
                .subscribe(object : OnDataV2Listener<UserB>(this) {
                    override fun getProgressBarContent() = "注册中..."

                    override fun onSuccess(value: NormalResponseB<UserB>) {
                        toast("注册成功")
                        closeThisPage()
                    }
                })

    }


    fun closeThisPage() {
        smsCounter.endCountTime()
        finish()
    }

    override fun onKeyBoardShow() {
        iv_app_logo.gone()
    }

    override fun onKeyBoardDismiss() {
        iv_app_logo.visible()
    }


}
